import { createRouter, createWebHistory } from 'vue-router'
import HomePage from '@/components/HOME/HomePage.vue'
import PageTop from '@/components/MainPage/PageTop.vue'
import login from '@/components/HOME/login.vue'
 const router=createRouter({
    history:createWebHistory(),
    routes:[
        {
            name:'denglu',
            path:"/HomePage",
            component:HomePage,
        },//重定向
        {
            path:"/",
            redirect:"/HomePage",
        },
        {
            name:"shouye",
            path:"/PageTop",
            component:PageTop,
            meta:{isAuth:true},
            children:[
                {
            path:"shangping",
            component:()=>import("@/components/MainPage/Commodity.vue"),
            meta:{isAuth:true},
            },{
                path:"changku",
                component:()=>import("@/components/MainPage/Warehouse.vue"),
                meta:{isAuth:true},
            },
            {
                path:"kehu",
                component:()=>import("@/components/MainPage/Cliet.vue"),
                meta:{isAuth:true},
            },
            {
                path:"gongyingshang",
                component:()=>import("@/components/MainPage/Supplier.vue"),
                meta:{isAuth:true},
            },
            {
                path:"gerenzhongxing",
                component:()=>import("@/components/MainPage/PersonalCenter.vue"),
                meta:{isAuth:true},
            }
            ]
        },
        {
            name:'zc',
            path:"/Login",
            component:login,
            meta:{isAuth:true},
        },
        
    ]
})
router.beforeEach((to, from, next) => {
    const token = sessionStorage.getItem('TOKEN');
    if (to.meta.isAuth) {
        console.log(token);
        if (token==='null'||!token) {
            alert("请先登录");
            next({ name: "denglu" });
        } else {
            next(); // 已登录，允许访问
        }
    } else {
        next(); // 不需要权限的页面直接放行
    }
});
// router.beforeEach((to,from,next)=>{
//     // console.log(to,from,sessionStorage.getItem('TOKEN'));
//     const a=sessionStorage.getItem('TOKEN')
//     if(to.meta.isAuth && !a){
//         console.log(11);
//         alert("账号或密码错误")
//         next({name:"denglu"})
//     }else{
//         console.log(22, !a);
//         next()
//     }
// })

export default router
